﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using StreetlightGames.SpaceShooter.Patterns;
using System.Reflection;
using System.Web;
using System.Collections.Specialized;
using StreetlightGames.XnaEngine;
using Microsoft.Xna.Framework;

namespace StreetlightGames.SpaceShooter
{
    [DataContract]
    public class PatternData
    {
        /// <summary>Gets or sets the PatternName value</summary>
        [DataMember]
        public string PatternName
        {
            get { return _patternName; }
            set { _patternName = value; }
        }
        private string _patternName;

        /// <summary>Gets or sets the Details value</summary>
        [DataMember]
        public string Details
        {
            get { return _details; }
            set { _details = value; }
        }
        private string _details;

        public PatternData()
        {
        }

        public PatternData(Pattern pattern)
        {
            string patternInfo = pattern.ToString();
            if (patternInfo.Contains(":"))
            {
                PatternName = patternInfo.Substring(0, patternInfo.IndexOf(':'));
                Details = patternInfo.Substring(patternInfo.IndexOf(':') + 2);
            }
            else
            {
                PatternName = patternInfo;
            }
        }

        public override string ToString()
        {
            if (!string.IsNullOrEmpty(Details))
            {
                return string.Format("{0}:{1}", PatternName, Details);
            }
            else
            {
                return PatternName;
            }
        }

        public Pattern CreatePattern()
        {
            string parameters = Details;
            if (!string.IsNullOrEmpty(parameters))
            {
                parameters = parameters.Replace(':', '=');
                parameters = parameters.Replace(',', '&');
            }
            Pattern pattern = Assembly.GetExecutingAssembly().CreateInstance(string.Format("StreetlightGames.SpaceShooter.Patterns.{0}", PatternName.Replace(" ", ""))) as Pattern;
            if (pattern is MoveToFormation)
            {
                return pattern;
            }
            if (!string.IsNullOrEmpty(parameters))
            {
                ContinueForward continueForward = pattern as ContinueForward;
                FlyInCircle flyInCircle = pattern as FlyInCircle;
                MoveToTarget moveToTarget = pattern as MoveToTarget;
                parameters = parameters.Replace(" ", "");
                NameValueCollection queryString = HttpUtility.ParseQueryString(parameters);
                if (continueForward != null)
                {
                    continueForward.Distance = float.Parse(HttpUtility.UrlDecode(queryString["Distance"]));
                }
                else if (flyInCircle != null)
                {
                    flyInCircle.Direction = float.Parse(HttpUtility.UrlDecode(queryString["Direction"]));
                    flyInCircle.Radius = float.Parse(HttpUtility.UrlDecode(queryString["Radius"]));
                    flyInCircle.TotalAngle = MathHelper.ToRadians(float.Parse(HttpUtility.UrlDecode(queryString["TotalAngle"])));
                }
                else if (moveToTarget != null)
                {
                    moveToTarget.Target = VectorHelper.CreateVector(HttpUtility.UrlDecode(queryString["Target"]));
                }
            }
            return pattern;
        }
    }
}
